package com.cw.controller;

import com.cw.common.api.ResultObject;
import com.cw.entity.SqlParam;
import com.cw.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;


@RestController
@RequestMapping("/CWJudgmentGenerator")
@Api(tags = "判断代码生成器")
public class JudgmentCodeController {

    @PostMapping("/JudgmentCodeGenerator")
    @ApiOperation("判断代码生成器")
    public ResultObject JudgmentCodeGenerator(@RequestBody SqlParam sqlParam) {
        AtomicReference<String> ans = new AtomicReference<>("");
        String param = sqlParam.getParam();
        List<String> splitlist1 = Arrays.asList(param.split("\\n"));
        splitlist1.forEach(item -> {
            List<String> itemSplitList = Arrays.asList(item.replaceAll("\t", " ").split(" "));
            String entityFiled = itemSplitList.get(0);
            String type = itemSplitList.get(1);
            List<String> splitList2 = Arrays.asList(entityFiled.split("_"));
            String newEntityFiled = splitList2.get(0);
            for (int i = 1; i < splitList2.size(); i++) {
                newEntityFiled += splitList2.get(i).substring(0, 1).toUpperCase() + splitList2.get(i).substring(1);
            }

            if (StringUtils.isEmpty(sqlParam.getEntity())) {
                if (type.toLowerCase().endsWith("date") || type.toLowerCase().endsWith("datetime")) {
                    ans.set(ans + "  <div>  &lt;if test=\"" + newEntityFiled + " != null and " + newEntityFiled + " != ''\"   &gt; <br>  " + " &nbsp;&nbsp;&nbsp;&nbsp;  AND " + itemSplitList.get(0) + " &lt;![CDATA[ &gt;= ]]&gt;  #{" + newEntityFiled + "}<br>" + "&lt;/if &gt;  <br><br></div>");
                } else if (type.toLowerCase().endsWith("int")) {
                    ans.set(ans + "<div>   &lt;if test=\"" + newEntityFiled + " != null and " + newEntityFiled + " != 0\"  &gt;    <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;    AND " + itemSplitList.get(0) + " =#{" + newEntityFiled + "}<br>" + "&lt;/if &gt; <br> <br></div>");
                } else if (type.toLowerCase().endsWith("string") || type.toLowerCase().endsWith("varchar")) {
                    ans.set(ans + "<div>   &lt;if test=\"" + newEntityFiled + " != null and " + newEntityFiled + " != ''\"  &gt;  <br> " + " &nbsp;&nbsp;&nbsp;&nbsp;    AND " + itemSplitList.get(0) + " =#{" + newEntityFiled + "}<br>" + "&lt;/if &gt;  <br><br></div>");
                }
            } else {
                if (type.toLowerCase().endsWith("date") || type.toLowerCase().endsWith("datetime")) {
                    ans.set(ans + "  <div>  &lt;if test=\"" + sqlParam.getEntity() + "." + newEntityFiled + " != null and " + sqlParam.getEntity() + "." + newEntityFiled + " != ''\"   &gt; <br>  " + " &nbsp;&nbsp;&nbsp;&nbsp;  AND " + itemSplitList.get(0) + " &lt;![CDATA[ &gt;= ]]&gt;  #{" + sqlParam.getEntity() + "." + newEntityFiled + "}<br>" + "&lt;/if &gt;  <br><br></div>");
                } else if (type.toLowerCase().endsWith("int")) {
                    ans.set(ans + "<div>   &lt;if test=\"" + sqlParam.getEntity() + "." + newEntityFiled + " != null and " + sqlParam.getEntity() + "." + newEntityFiled + " != 0\"  &gt;    <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;    AND " + itemSplitList.get(0) + " =#{" + sqlParam.getEntity() + "." + newEntityFiled + "}<br>" + "&lt;/if &gt; <br> <br></div>");
                } else if (type.toLowerCase().endsWith("string") || type.toLowerCase().endsWith("varchar")) {
                    ans.set(ans + "<div>   &lt;if test=\"" + sqlParam.getEntity() + "." + newEntityFiled + " != null and " + sqlParam.getEntity() + "." + newEntityFiled + " != ''\"  &gt;  <br> " + " &nbsp;&nbsp;&nbsp;&nbsp;    AND " + itemSplitList.get(0) + " =#{" + sqlParam.getEntity() + "." + newEntityFiled + "}<br>" + "&lt;/if &gt;  <br><br></div>");
                }
            }
        });
        System.out.println(ans.get());
        return ResultObject.successReturn(ans);
    }
//    public ResultObject JudgmentCodeGenerator(@RequestBody SqlParam sqlParam) {
//        String param = sqlParam.getParam();
//        System.out.println(param);
//        String s1 = "", s2 = "", s3 = "", s4 = "";
//        String ans = "";
//        //s3为新字符串，s4为原字符串，ans字符串拼接为最终输出结果
//
//        int locLine = -1;            //下划线的位置
//        int locComma = 0;           //空格的位置
//
//        List<String> oriCode = Arrays.asList(param.split(","));
//        for (int i = 0; i < oriCode.size(); i++) {
//            param = oriCode.get(i);
//            param.trim();
//            locLine = param.indexOf("_");
//            locComma = param.indexOf(" ");
//            s4 = param.substring(0, locComma).toUpperCase();
//            if (locLine != -1) {
//                s1 = param.substring(0, locLine);
//                s1 = s1.toLowerCase();
//                s2 = param.substring(locLine + 1, locComma);
//                s2 = s2.toLowerCase();
//                s3 = s2.substring(0, 1).toUpperCase() + s2.substring(1);
//                s3 = s1 + s3;
//            } else {
//                s3 = param.substring(0, locComma).toLowerCase();
//            }
//
//            if (param.toLowerCase().endsWith("string") && (param.indexOf("NAME") != -1)) {
//                ans = ans + "<div>   &lt;  if test=\"" + s3 + " != null and " + s3 + " != ''\"&gt;  <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;AND " + s4 + " LIKE '%${" + s3 + "}%'<br>" + "&lt;  /if &gt;  <br><br>  </div>";
//            } else if (param.toLowerCase().endsWith("date") || param.toLowerCase().endsWith("datetime")) {
//                ans = ans + "  <div>  &lt;   if test=\"" + s3 + " != null and " + s3 + " != ''\"   &gt; <br>  " + " &nbsp;&nbsp;&nbsp;&nbsp;  AND " + s4 + " &lt;  ![CDATA[ &gt;= ]] &gt;  #{" + s3 + "}<br>" + "&lt;  /if &gt;  <br><br></div>";
//            } else if (param.toLowerCase().endsWith("int")) {
//                ans = ans + "<div>   &lt;  if test=\"" + s3 + " != null and " + s3 + " != 0\"  &gt;    <br>" + "&nbsp;&nbsp;&nbsp;&nbsp;    AND " + s4 + " =#{" + s3 + "}<br>" + "&lt;  /if &gt; <br> <br></div>";
//            } else if (param.toLowerCase().endsWith("string")) {
//                ans = ans + "<div>   &lt;  if test=\"" + s3 + " != null and " + s3 + " != ''\"  &gt;  <br> " + " &nbsp;&nbsp;&nbsp;&nbsp;    AND " + s4 + " =#{" + s3 + "}<br>" + "&lt;  /if &gt;  <br><br></div>";
//            }
//        }
//        System.out.println(ans);
//        return ResultObject.successReturn(ans);
//    }
}
